package com.qiwenshare.file.controller;
import com.alibaba.fastjson.JSON;
import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.common.util.security.JwtUser;
import com.qiwenshare.common.util.security.SessionUtil;
import com.qiwenshare.file.api.*;
import com.qiwenshare.file.component.AsyncTaskComp;
import com.qiwenshare.file.dto.file.DecryptEncryptFileDTO;
import com.qiwenshare.file.dto.decryptfile.BatchDecryptEncryptFileDTO;
import com.qiwenshare.file.dto.decryptfile.DecryptFileDTO;
import com.qiwenshare.file.vo.file.DecryptFileListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "decryptfile", description = "文件加密后会进入加密文件夹，该接口主要是对加密文件夹的文件进行管理")
@RestController
@Slf4j
@RequestMapping("/decryptfile")
public class DecryptFileController {
    @Resource
    IDecryptFileService decryptFileService;
    @Resource
    IUserFileService userFileService;
    @Resource
    IUserService userService;
    @Resource
    IFileService fileService;
    @Resource
    IFiletransferService filetransferService;
    @Resource
    AsyncTaskComp asyncTaskComp;


    public static final String CURRENT_MODULE = "加密文件夹文件接口";


    @Operation(summary = "解密文件", description = "解密文件", tags = {"decryptfile"})
    @RequestMapping(value = "/decryptonefile", method = RequestMethod.POST)
    @MyLog(operation = "解密文件", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult decryptFile(@RequestBody DecryptFileDTO decryptFileDto) {
        JwtUser sessionUserBean = SessionUtil.getSession();
        decryptFileService.decryptfile(decryptFileDto.getEncryptBatchNum(), decryptFileDto.getFilePath(), sessionUserBean.getUserId());
        return RestResult.success().message("解密成功！");
    }


    @Operation(summary = "批量解密文件", description = "批量解密文件", tags = {"decryptfile"})
    @RequestMapping(value = "/batchdecrypt", method = RequestMethod.POST)
    @MyLog(operation = "批量解密文件", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> batchDecryptEncryptFile(@RequestBody BatchDecryptEncryptFileDTO batchDecryptEncryptFileDto) {
        JwtUser sessionUserBean = SessionUtil.getSession();
        List<DecryptFileDTO> decryptFileDTOList = JSON.parseArray(batchDecryptEncryptFileDto.getDecryptFileIds(), DecryptFileDTO.class);
        for(DecryptFileDTO decryptFileDto :decryptFileDTOList){
            decryptFileService.decryptfile(decryptFileDto.getEncryptBatchNum(), decryptFileDto.getFilePath(), sessionUserBean.getUserId());
        }
        return RestResult.success().data("批量解密成功");
    }

    @Operation(summary = "解密文件列表", description = "解密文件列表", tags = {"decryptfile"})
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RestResult<List<DecryptFileListVo>> getDecryptFileList() {
        JwtUser sessionUserBean = SessionUtil.getSession();
        RestResult<List<DecryptFileListVo>> restResult = new RestResult<List<DecryptFileListVo>>();
        List<DecryptFileListVo> decryptFileList = decryptFileService.selectDecryptFileList(sessionUserBean.getUserId());
        restResult.setData(decryptFileList);
        restResult.setSuccess(true);

        return restResult;
    }


}















